package com.yzf.demo.demo.Thread.ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 于占峰 on 2020/4/4/004.
 * 实现线程池监控测试类
 */
public class ThreadPollSelfDemo implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(10);//休眠十秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        //构建一个固定数量的线程池
        ExecutorService excutorService = ExecutorsSelf.newFixedThreadPool(3);
        //构建一个动态扩展的线程池
        ExecutorService excutorService1 = ExecutorsSelf.newCachedThreadPool();
        //创建一个只有一个线程的线程池
        ExecutorService excutorService2 = ExecutorsSelf.newSingleThreadExecutor();
        //创建一个带定时调度任务的线程池
        ExecutorService excutorService3 = ExecutorsSelf.newScheduledThreadPool(5);
        //创建一个自行实现的线程池

        //循环利用线程池内的线程
        for (int i = 0; i < 100; i++) {
            excutorService.execute(new ThredPoolDemo());
//            excutorService1.execute(new ThredPoolDemo());
//            excutorService2.execute(new ThredPoolDemo());
//            excutorService3.execute(new ThredPoolDemo());
        }
        excutorService.shutdown();//停止线程池
    }
}
